package com.army.cheatsheets.adapter;

import com.army.cheatsheets.ResourceTable;
import com.army.cheatsheets.databean.Item;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class ItemProvider extends RecycleItemProvider {

    private List<Item> list;
    private AbilitySlice slice;

    public ItemProvider(List<Item> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder;

        String dataType = list.get(i).getDataType();
        if("category".equalsIgnoreCase(dataType)){
            if (null == component) {
                component = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_category, null, false);

                viewHolder = new ViewHolder();
                viewHolder.category = (Text) component.findComponentById(ResourceTable.Id_category);
                component.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) component.getTag();
            }
            viewHolder.category.setText(list.get(i).getName());
        }else{
            if (null == component) {
                component = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item, null, false);

                viewHolder = new ViewHolder();
                viewHolder.standard = (Text) component.findComponentById(ResourceTable.Id_standard);
                viewHolder.time = (Text) component.findComponentById(ResourceTable.Id_time);
                component.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) component.getTag();
            }
            viewHolder.standard.setText(list.get(i).getName());
            viewHolder.time.setText(list.get(i).getTime());
        }

        return component;
    }

    class ViewHolder {
        Text category;
        Text standard;
        Text time;
    }

}
